package com.yohann.designPattern.observer.data;

import com.yohann.designPattern.observer.observer.Observer;

import java.util.ArrayList;

/**
 * <p>
 * 天气信息
 * </p>
 *
 * @author Yohann
 * @since 2021/2/12 17:09
 */
public class WeatherData implements Subject {
    private float temperature;
    private float pressure;
    private float humidity;
    private ArrayList<Observer> observers;

    public WeatherData() {
        observers = new ArrayList<>();
    }

    public void setData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        this.notifyObserver();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(temperature, pressure, humidity);
        }
    }
}